<template>
	<div class="system-dic-dialog-container">
		<el-dialog :title="state.dialog.title" v-model="state.dialog.isShowDialog" width="769px" @close="clean">
			<el-form ref="credentialDialogFormRef" :rules="rules" :model="state.ruleForm" size="default" label-width="90px">
				<el-row :gutter="35">
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="名称" prop="name">
							<el-input v-model="state.ruleForm.name" placeholder="请输入名称" clearable></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="配置文件" prop="meta.config">
							<el-input v-model="state.ruleForm.meta.config" :rows="8" type="textarea" placeholder="请输入配置文件"></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="描述" prop="description">
							<el-input v-model="state.ruleForm.description" type="textarea" placeholder="请输入描述" maxlength="200"></el-input>
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="onCancel" size="default">取 消</el-button>
					<el-button type="primary" @click="onSubmit" size="default">{{ state.dialog.submitTxt }}</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script setup lang="ts" name="systemDicDialog">
import { reactive, ref } from 'vue';
import { resourceApi } from '/@/api/resource';
import Constants from '/@/constant/constants';

// 定义子组件向父组件传值/事件
const emit = defineEmits(['refresh']);

const rules = reactive({
	name: [{ required: true, message: '名称必填', trigger: 'blur' }],
	'meta.config': [{ required: true, message: '配置必填', trigger: 'blur' }],
});

// 定义变量内容
const credentialDialogFormRef = ref();
const state = reactive({
	ruleForm: {
		id: undefined,
		type: Constants.RESOURCE_TYPE.K8S_CLUSTER,
		name: '', // 资源名称
		description: '', // 描述
		meta: {
			config: '', // 配置文件
		},
	},
	dialog: {
		isShowDialog: false,
		type: '',
		title: '',
		submitTxt: '',
	},
});

// 打开弹窗
const openDialog = (type: string, row: Object) => {
	if (type === 'edit') {
		state.ruleForm = row;
		state.dialog.title = '修改配置';
		state.dialog.submitTxt = '修 改';
	} else {
		state.dialog.title = '新增配置';
		state.dialog.submitTxt = '新 增';
	}
	state.dialog.isShowDialog = true;
};

// 清空表单
const clean = () => {
	state.ruleForm = {
		id: undefined,
		type: Constants.RESOURCE_TYPE.K8S_CLUSTER,
		name: '', // 资源名称
		description: '', // 描述
		meta: {
			config: '', // 配置文件
		},
	};
};

// 关闭弹窗
const closeDialog = () => {
	state.dialog.isShowDialog = false;
};
// 取消
const onCancel = () => {
	closeDialog();
};
// 提交
const onSubmit = () => {
	credentialDialogFormRef.value.validate((valid: boolean) => {
		if (valid) {
			resourceApi()
				.addResource(state.ruleForm)
				.then(() => {
					closeDialog();
					emit('refresh');
					ElMessage.success('操作成功');
				})
				.catch((err) => {
					ElMessage.error(err.msg);
				});
		}
	});
};

// 暴露变量
defineExpose({
	openDialog,
});
</script>
